package 单周赛.september;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @
 * @date 2024/04/21
 */
public class 第394场单周赛 {

    public static void main(String[] args) {


        System.out.println(numberOfSpecialChars2("aaAbcBC"));
    }

    public int numberOfSpecialChars(String word) {
        Set<Character> set = new HashSet<>();
        Set<Character> ans = new HashSet<>();
        for (char c : word.toCharArray()) {

            if (set.contains((char) (c ^ 32))) {
                ans.add((char) (c | 32));
            }
            set.add(c);

        }

        return ans.size();
    }


    public static int numberOfSpecialChars2(String word) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (Character.isUpperCase(c)) {
                map.put(c, Math.min(map.getOrDefault(c, Integer.MAX_VALUE), i));
            } else {
                map.put(c, i);
            }
        }
        int ans = 0;
        for (int i = 0; i < 26; i++) {
            char lowC = (char) ('a' + i);
            char upC = (char) (lowC ^ 32);
            if (map.containsKey(lowC) && map.containsKey(upC)) {

                if (map.get(lowC) < map.get(upC)) {
                    ans++;
                }
            }
        }

        return ans;
    }
}
